package com.cj.webapp.user.dao;

import com.cj.webapp.common.utils.MybatisUtil;
import com.cj.webapp.user.dto.UserDto;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * Created by dell on 2018/1/12.
 */
public class UserDao {
    private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();
    //创建能执行映射文件中sql的sqlSession
    SqlSession session = sessionFactory.openSession();
    UserMapper userMapper = session.getMapper(UserMapper.class);

    /**
     * 查询当前登录用户
     * @param dto
     * @return
     */
    public UserDto findUserByName(UserDto dto){
        UserDto userDto=null;
        try {
            userDto=userMapper.findUserByName(dto);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.commit();
            //session.close();
        }
        return  userDto;
    }

    /**
     * 查询当前登录用户
     * @param dto
     * @return
     */
    public List<UserDto> findUserByPhone(UserDto dto){
        List<UserDto> userDto=null;
        try {
            userDto=userMapper.findUserByPhone(dto);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.commit();
            //session.close();
        }
        return  userDto;
    }

    /**
     * 用户注册
     * @param dto
     * @return
     */
    public  int addUser(UserDto dto){
        Integer u = null;
        try{
            u=userMapper.addUser(dto);
            session.commit();
            session.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return u;
    }



}
